package core
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;

	/**
	 * @author lonely
	 * @date Feb 6, 2013
	 */
	public class Util
	{
		public function Util()
		{
		}
		
		/**
		 * 获取2点中的一点位置,x取中间,y取差值2倍
		 * @param a 起始点
		 * @param b 终点
		 * @return 
		 * 
		 */		
		public static function getCenter(a:Point,b:Point,v:Number = 1.5):Point {
			var diffX:Number = b.x - a.x;
			var diffY:Number = b.y - a.y;
			if(b.y > a.y){
				diffY = a.y - b.y;
			}
			var result:Point = new Point(0,0);
			var halfX:Number = diffX >> 1;
			result.x = a.x + halfX;
			result.y = a.y + diffY * v;
			if(result.y < -300){
				result.y = -300;
			}
			return result;
		}
		
		/**
		 * 分解数据 
		 * @param total
		 * @return cache 返回一个数组每个object代表一个属性,v代码具体的item值,r代表数量
		 * 
		 */		
		public static function split(total:Number):Array {
			var count:int = Config.CHIP_LIST.length;
			var cache:Array = [];
			var i:int = count - 1;
			for(; i >= 0; i--){
				var item:Object = Config.CHIP_LIST[i];
				var v:Number = item.value;
				var r:int = (total + 0.00000000001) / v;// 防止出现错误
				if(r > 0){
					cache.push({v:item,r:r});
					total = total - r * v; 
					total = fixedNumber(total);
					if(total == 0){
						break;
					}
				}
			}
			cache.reverse();
			return cache;
		}
		
		public static function fixedNumber(value:Number,p:int = 1):Number {
			var r:int = 100;
			value = value * r;
			var result:Number = Number(Math.round(value).toFixed(p));
			result = result / r;
			return result;
		}
		
		public static function removeChild(value:DisplayObject):void {
			if(!value){
				return;
			}
			if(!value.parent){
				return;
			}
			value.parent.removeChild(value);
			/*if(value.parent.contains(value)){
				
			}*/
		}
		
		public static function removeAllChildren(value:DisplayObjectContainer):void {
			if(!value){
				return;
			}
			while(value.numChildren > 0){
				value.removeChildAt(0);
			}
		}
	}
}